package com.happyface.touchevent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;

/**
 * Created by hxm on 2017/6/26.
 * 描述：
 */
public class HLinearLayout extends LinearLayout {
    private boolean isScroll = false;
    private int mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    private float lastY;

    public HLinearLayout(Context context) {
        super(context);
    }

    public HLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = ev.getY();
                isScroll = false;
                Log.i("onInterceptTouchEvent", "ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(ev.getY() - lastY) > mTouchSlop) {
                    isScroll = true;
                    Log.i("onInterceptTouchEvent", "ACTION_MOVE_TRUE");
                } else {
                    Log.i("onInterceptTouchEvent", "ACTION_MOVE_FALSE");
                    isScroll = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.i("onInterceptTouchEvent", "ACTION_UP");
                isScroll = false;
                break;
        }
        return isScroll;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("onTouchEvent", "ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("onTouchEvent", "ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("onTouchEvent", "ACTION_UP");
                break;
        }
        return super.onTouchEvent(event);
    }
}
